# !usr/bin/env python
# -*-coding:utf-8 -*-

# @FileName: worker_.py
# @Author:tian
# @Time:06/23/2021

import pika
import time

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')


def callback(ch, method, properties, body):
    print(" [x] Received %r" % body.decode())
    ch.basic_ack(delivery_tag=method.delivery_tag)
 # 如果该消费者的channel上未确认的消息数达到了prefetch_count数，则不向该消费者发送消息
 # prefetchCount在手动ack的情况下才生效，自动ack不生效。
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback,auto_ack=False)

channel.start_consuming()